package com.bupt.fmmall.service.Impl;

import com.bupt.fmmall.dao.CustomerDao;
import com.bupt.fmmall.entity.Customer;
import com.bupt.fmmall.service.CustomerService;
import com.bupt.fmmall.utils.MD5Utils;
import com.bupt.fmmall.vo.ResultVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    CustomerDao customerDao;

    //顾客注册
    @Override
    public int registerCustomer(Customer customer) {
        Customer customer1=customerDao.selectCustomer(customer.getCustomerUserName());
        if (customer1==null){
            //没有重名的用户，可以注册
            //通过MD5加密顾客密码
            Customer customer2=new Customer();
            String md5pwd=MD5Utils.md5(customer.getCustomerPassword());
            customer2.setCustomerAddress(customer.getCustomerAddress());
            customer2.setCustomerPassword(md5pwd);
            customer2.setCustomerPhoneNumber(customer.getCustomerPhoneNumber());
            customer2.setCustomerRealName(customer.getCustomerRealName());
            customer2.setCustomerUserName(customer.getCustomerUserName());
            customerDao.insertCustomer(customer2);
            return 200;
        }else {
            //存在重名的用户
            return 601;
        }
    }

    @Override
    //顾客登录
    public ResultVO loginCustomer(String customerUserName, String customerPassword) {
        Customer customer=customerDao.selectCustomer(customerUserName);
        if (customer==null){
            //用户不存在
            return new ResultVO(601,"用户不存在",null);
        }else if (!(customer.getCustomerPassword().equals(MD5Utils.md5(customerPassword)))){
            //密码错误
            return new ResultVO(601,"密码错误",null);  
        }else{
            //登录成功
            return new ResultVO(200,"登录成功",customer);
        }
    }

    //获取用户信息
    @Override
    public ResultVO getCustomerInfoByUsername(String customerUserName) {
        Customer customer=new Customer();
        customer=customerDao.selectCustomer(customerUserName);
        return new ResultVO(200,"success",customer);
    }

    @Override
    public ResultVO changeReceiptInfo(Customer customer) {
        int i=customerDao.updateCustomer(customer);
        if (i>0){
            return new ResultVO(200,"success",null);
        }else {
            return new ResultVO(601,"修改收货信息失败",null);
        }
    }

    @Override
    public ResultVO changeCusInfo(Customer customer) {
            //通过MD5加密顾客密码
            String md5pwd=MD5Utils.md5(customer.getCustomerPassword());
            customer.setCustomerPassword(md5pwd);
            int i =customerDao.updateCustomerInfo(customer);
            if (i>0){
                return new ResultVO(200,"success",customer);
            }else{
                return new ResultVO(601,"更新失败",null);
            }
    }
}
